import md5 from 'crypto-js/md5';
import { codeToText } from "element-china-area-data";

const TokenKey = 'zh-token'
const UserID = 'userId';

//德信账户
export const dexinBankNumber = '4104310101800102019999';

export function getToken() {
    return localStorage.getItem(TokenKey);
}

export function setToken(token) {
    return localStorage.setItem(TokenKey, token);
}

export function removeToken() {
    return localStorage.removeItem(TokenKey);
}

export function getUserId() {
    return localStorage.getItem(UserID);
}
export function setUserId(id) {
    return localStorage.setItem(UserID, id);
}
//加密平台标识
const OS = 'web';

//加密盐
const SALT = 'emhhHaC';

//获取请求头
export const getHeader = () => {
    const timeStamp = Date.now() + '';
    const sign = '01' + md5(timeStamp + SALT);
    let obj = {
        'os': OS,
        'x-time': timeStamp,
        'x-sign': sign,
    };
    if (getToken()) {
        obj['token'] = getToken();
    }
    if (getUserId()) {
        obj['userId'] = getUserId();
    }
    return obj;
}

//把区域码转成汉字
export const getCodeToText = (codeArray) => {
    let area = [];
    switch (codeArray.length) {
        case 1:
            area.push(codeToText[codeArray[0]])
            break;
        case 2:
            area.push(codeToText[codeArray[0]])
            area.push(codeToText[codeArray[1]])
            break;
        case 3:
            area.push(codeToText[codeArray[0]])
            area.push(codeToText[codeArray[1]])
            area.push(codeToText[codeArray[2]])
            break;
        default:
            break;
    }
    return area;
}


//上传图片地址
export const uploadUrl = 'https://api.zhgylgl.com/api/uploadImg';

//深克隆方法
export const deepClone = (obj) => {
    let objClone = Array.isArray(obj) ? [] : {};
    if (obj && typeof obj === "object") {
        for (var key in obj) {
            if (obj.hasOwnProperty(key)) {
                //判断ojb子元素是否为对象，如果是，递归复制
                if (obj[key] && typeof obj[key] === "object") {
                    objClone[key] = deepClone(obj[key]);
                } else {
                    //如果不是，简单复制
                    objClone[key] = obj[key];
                }
            }
        }
    }
    return objClone;
}

//返回一个秒为单位的时间戳 ,转换为2024/01/01 08:00:00
export function transformDate(time) {
    const timestamp = time;
    const date = new Date(timestamp * 1000);
    return date.toLocaleString();
}


//下载后端返回的文件
export const downloadFile = (url) => {
    var oReq = new XMLHttpRequest();
    // The Endpoint of your server
    const fileName = (url + '').replace('http://img.zhgylgl.com/', '');
    var URLToPDF = url;
    // Configure XMLHttpRequest
    oReq.open("GET", URLToPDF, true);
    // Important to use the blob response type
    oReq.responseType = "blob";
    // When the file request finishes
    // Is up to you, the configuration for error events etc.
    oReq.onload = function () {
        // Once the file is downloaded, open a new window with the PDF
        // Remember to allow the POP-UPS in your browser
        var file = new Blob([oReq.response], {
            type: "application/pdf"
        });
        // Generate file download directly in the browser !
        // saveAs(file, "mypdffilename.pdf");
        saveAs(file, decodeURIComponent(fileName));
    };
    oReq.send();
}

//姓名和手机号中间四位替换为*
export const maskString = (input = '') => {
    return input;
    if (!input) return '';

    // 判断是否为手机号
    if (/^1[3-9]\d{9}$/.test(input)) {
        // 手机号中间四位替换为*
        return input.slice(0, 3) + '****' + input.slice(7);
    }
    //特殊处理汉字名称后面带数字情况
    let lastNum = input.slice(-1);
    if(input.length < 6 && !isNaN(lastNum)){
        let preNam = input.slice(0, -1);
        //此时再判断前面得是否为汉字
        if(/^[\u4e00-\u9fa5]+$/.test(preNam)){
            const length = preNam.length;
            if (length === 2) {
                // 二字姓名，第二位替换为*
                return preNam[0] + '*' + lastNum;
            } else if (length === 3) {
                // 三字姓名，第二位替换为*
                return preNam[0] + '*' + preNam[2] + lastNum;
            } else if (length === 4) {
                // 四字姓名，中间两位替换为*
                return preNam[0] + '**' + preNam[3] + lastNum;
            }
        }
    }


    // 判断是否为汉字姓名
    if (/^[\u4e00-\u9fa5]+$/.test(input)) {
        const length = input.length;
        if (length === 2) {
            // 二字姓名，第二位替换为*
            return input[0] + '*';
        } else if (length === 3) {
            // 三字姓名，第二位替换为*
            return input[0] + '*' + input[2];
        } else if (length === 4) {
            // 四字姓名，中间两位替换为*
            return input[0] + '**' + input[3];
        }
    }

    // 银行卡号 子账号中间四位替换为*
    return input.slice(0, 5) + '********' + input.slice(13);

    // 如果不是手机号或汉字姓名，返回原字符串
    return input;
}
